﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FisheryPlatform.Search.Core.Resource
{
    public class FileResource : AbstractStreamResource
    {
        // Fields
        private string basePath;
        private string filePath;
        private readonly Stream stream;

        // Methods
        public FileResource(CustomUri resource)
        {
            this.stream = this.CreateStreamFromUri(resource, AbstractResource.DefaultBasePath);
        }

        public FileResource(string resourceName)
        {
            this.stream = this.CreateStreamFromPath(resourceName, AbstractResource.DefaultBasePath);
        }

        public FileResource(CustomUri resource, string basePath)
        {
            this.stream = this.CreateStreamFromUri(resource, basePath);
        }

        public FileResource(string resourceName, string basePath)
        {
            this.stream = this.CreateStreamFromPath(resourceName, basePath);
        }

        private void CheckFileExists(string path)
        {
            if (!File.Exists(path))
            {
                throw new ResourceException(string.Format("File {0} could not be found", new FileInfo(path).FullName));
            }
        }

        public override IResource CreateRelative(string resourceName)
        {
            return new FileResource(resourceName, this.basePath);
        }

        private Stream CreateStreamFromPath(string filePath, string basePath)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException("filePath");
            }
            if (basePath == null)
            {
                throw new ArgumentNullException("basePath");
            }
            if (!Path.IsPathRooted(filePath) || !File.Exists(filePath))
            {
                filePath = Path.Combine(basePath, filePath);
            }
            this.CheckFileExists(filePath);
            this.filePath = Path.GetFileName(filePath);
            this.basePath = Path.GetDirectoryName(filePath);
            return File.OpenRead(filePath);
        }

        private Stream CreateStreamFromUri(CustomUri resource, string basePath)
        {
            if (resource == null)
            {
                throw new ArgumentNullException("resource");
            }
            if (basePath == null)
            {
                throw new ArgumentNullException("basePath");
            }
            if (!resource.IsFile)
            {
                throw new ArgumentException("The specified resource is not a file", "resource");
            }
            return this.CreateStreamFromPath(resource.Path, basePath);
        }

        public override string ToString()
        {
            return string.Format("FileResource: [{0}] [{1}]", this.filePath, this.basePath);
        }

        // Properties
        public override string FileBasePath
        {
            get
            {
                return this.basePath;
            }
        }

        protected override Stream Stream
        {
            get
            {
                return this.stream;
            }
        }
    }


}
